/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spring;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import phys.CollisionResolver;
import phys.State;
import vec.Vector3f;

/**
 *
 * @author Vic
 */
public class Block extends Element {

    Rectangle bounds;

    public Block(Rectangle bounds) {
        state = new State();
        setMass(2);
        setPosition(new Vector3f(bounds.x, bounds.y,0));
        state.setMass(Float.POSITIVE_INFINITY);
        this.bounds = bounds;
        CollisionResolver.add(this);
    }

    @Override
    public Rectangle getBounds() {
        bounds.setLocation((int)getPosition().x-bounds.width/2,(int) getPosition().y-bounds.height/2);
        return bounds;
    }


    @Override
    public boolean collides(Vector3f point) {
        bounds.setLocation((int)getPosition().x-bounds.width/2,(int) getPosition().y-bounds.height/2);
        return (bounds.contains(new Point.Float(point.x, point.y)));
    }

    @Override
    public void draw(Graphics g, Vector3f origen) {
        bounds.setLocation((int)getPosition().x-bounds.width/2,(int) getPosition().y-bounds.height/2);
        g.drawRect((int) bounds.getMinX(), (int) (2 * origen.y - bounds.getMinY() - bounds.height),
                bounds.width, bounds.height);
    }

    @Override
    public Vector3f getMaxInDir(Vector3f dir) {
        Vector3f center = new Vector3f((float) bounds.getCenterX(), (float) bounds.getCenterY(), 0);
        float xint = Math.abs(((float)bounds.getWidth())/ dir.x),
                yint = Math.abs(((float) bounds.getWidth()) / dir.y);        
        dir.normalize();
        if (xint < yint) {
            dir.scale(xint/2);
        } else {
            dir.scale(yint/2);
        }
        center.add(dir);
        return center;
    }

    @Override
    public boolean isPassive() {
        return false;
    }


    @Override
    public Vector3f getAcceleration(State state, float t) {
        return new Vector3f();
    }

    @Override
    public Vector3f getAppliedForce(Element acc, float t) {
        return new Vector3f();
    }
}
